package com.xyht.medicine_management_system.modules.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.activity.entity.MmsActivity;
import com.xyht.medicine_management_system.modules.activity.entity.req.MmsActivityReq;
import com.xyht.medicine_management_system.modules.activity.entity.resp.MmsActivityResp;
import com.xyht.medicine_management_system.modules.activity.mapper.MmsActivityMapper;
import com.xyht.medicine_management_system.modules.activity.service.MmsActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-11-29
 */
@Service
public class MmsActivityServiceImpl extends ServiceImpl<MmsActivityMapper, MmsActivity> implements MmsActivityService {
    @Resource
    private MmsActivityMapper mmsActivityMapper;

    @Override
    public ResponseResult addActivity(MmsActivityReq activityReq) {
        if (isNullOrEmpty(activityReq.getName(), activityReq.getType(), activityReq.getStartDate(), activityReq.getEndDate())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsActivity activity = new MmsActivity();
        BeanUtils.copyProperties(activityReq, activity);

        mmsActivityMapper.insert(activity);

        return CommonResult.success();
    }

    @Override
    public ResponseResult delActivity(MmsActivityReq activityReq) {
        if (isNullOrEmpty(activityReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsActivity activity = mmsActivityMapper.selectById(activityReq.getId());
        if (isNullOrEmpty(activity)) {
            return CommonResult.failed(CommonCodeEnum.ACTIVITY_NOT_EXIST);
        }

        mmsActivityMapper.deleteById(activityReq.getId());

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateActivity(MmsActivityReq activityReq) {
        if (isNullOrEmpty(activityReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsActivity activity = mmsActivityMapper.selectById(activityReq.getId());
        if (isNullOrEmpty(activity)) {
            return CommonResult.failed(CommonCodeEnum.ACTIVITY_NOT_EXIST);
        }

        BeanUtils.copyProperties(activityReq, activity);

        mmsActivityMapper.updateById(activity);

        return CommonResult.success();
    }

    @Override
    public ResponseResult listActivity(Integer pageNum, Integer pageSize, String searchStr) {
        LambdaQueryWrapper<MmsActivity> queryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.like(MmsActivity::getName, searchStr).or()
                    .like(MmsActivity::getIntroduction, searchStr).or()
                    .like(MmsActivity::getContactInfo, searchStr);
        }

        Page<MmsActivity> page = new Page<>(pageNum, pageSize);
        mmsActivityMapper.selectPage(page, queryWrapper);

        List<MmsActivityResp> activityRespList = page.getRecords().stream().map(activity -> {
            MmsActivityResp activityResp = new MmsActivityResp();
            BeanUtils.copyProperties(activity, activityResp);
            return activityResp;
        }).collect(Collectors.toList());

        return CommonResult.success(activityRespList, (int) page.getTotal());
    }

    @Override
    public ResponseResult activityDetail(String activityId) {
        if (isNullOrEmpty(activityId)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsActivity activity = mmsActivityMapper.selectById(activityId);
        if (isNullOrEmpty(activity)) {
            return CommonResult.failed(CommonCodeEnum.ACTIVITY_NOT_EXIST);
        }

        MmsActivityResp activityResp = new MmsActivityResp();
        BeanUtils.copyProperties(activity, activityResp);

        return CommonResult.success(activityResp);
    }


}
